/*
 * GroupsDao.java
 *
 * Created on November 9, 2008, 7:37 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package jollylengkono.skripsi.dao;

import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import jollylengkono.skripsi.iface.Crud;
import jollylengkono.skripsi.model.Groups;

/**
 *
 * @author JollkY
 */
public class GroupsDao implements Crud {
    
    private int state;
    private Groups group;
    private List groups;
    private EntityManagerFactory emf = Persistence.createEntityManagerFactory("kmPU");
    private EntityManager em = emf.createEntityManager();
    
    /** Creates a new instance of GroupsDao */
    public GroupsDao() {
    }

    
    public int create(Object obj) {
        try {
            em.getTransaction().begin();
            em.persist(obj);
            em.getTransaction().commit();

            this.state = 1;
        } catch (Exception ex) {
            ex.printStackTrace();
            em.getTransaction().rollback();

            this.state = 0;
        } finally {
            em.close();
        }
        return state;
    }

    
    public List<Object> getList() {
        groups = new ArrayList();
        try {
            em.getTransaction().begin();
            groups = em.createQuery("SELECT group FROM Groups group").getResultList();
            em.getTransaction().commit();
        } catch (Exception ex) {
            ex.printStackTrace();
            em.getTransaction().rollback();
        } finally {
            em.close();
        }
        return groups;
    }

    
    public Object read(long id) {
        group = new Groups();
        try {
            em.getTransaction().begin();
            group = em.find(Groups.class, id);
            em.getTransaction().commit();
        } catch (Exception ex) {
            ex.printStackTrace();
            em.getTransaction().rollback();
        } finally {
            em.close();
        }
        return group;
    }

    
    public int update(Object obj) {
        group = new Groups();
        try {
            Groups objGrp = (Groups) obj;
            em.getTransaction().begin();
            group = em.find(Groups.class, objGrp);
            group.setCreated(objGrp.getCreated());
            group.setDescription(objGrp.getDescription());
            group.setGroupName(objGrp.getGroupName());
            group.setListObject(objGrp.getListObject());
            group.setStatus(objGrp.getStatus());
            em.getTransaction().commit();

            this.state = 1;
        } catch (Exception ex) {
            ex.printStackTrace();
            em.getTransaction().rollback();

            this.state = 0;
        } finally {
            em.close();
        }
        return state;
    }

    
    public int delete(long id) {
        group = new Groups();
        try {
            em.getTransaction().begin();
            group = em.find(Groups.class, id);
            em.remove(group);
            em.getTransaction().commit();

            this.state = 1;
        } catch (Exception ex) {
            ex.printStackTrace();
            em.getTransaction().rollback();

            this.state = 0;
        } finally {
            em.close();
        }
        return state;
    }
    
}
